# -*- coding: utf-8 -*-
"""
Created on Sat Dec 08 18:25:43 2012

@author: Winand
"""

import hashlib, sys, json

def main():
    if len(sys.argv)!=3:
        print "Use 'python adduser.py <user> <password>' to add new user"
        return
    if not (5 <= len(sys.argv[1]) <= 15):
        print "Error: username length must be between 5 and 15"
        return
    if not (6 <= len(sys.argv[2]) <= 15):
        print "Error: password length must be between 6 and 15"
        return
    authinf = {}
    try: #load auth data
        f = open("authdat", 'r')
        authinf = json.load(f)
    except: print "Error: load auth data failed, new file will be created" 
    if sys.argv[1] in authinf:
        print "Error: user already exists"
        return
    authinf[sys.argv[1]] = hashlib.sha1(sys.argv[1]+sys.argv[2]).hexdigest()
    try: #save auth data
        f = open("authdat", 'w')
        json.dump(authinf, f)
    except: print "Error: save auth data failed"
    print "User", sys.argv[1], "successfully added"

if __name__ == '__main__':
    main()

#from services.tracker import tracker
#
#t=tracker.tracker()
#t.addWebpage("ggg")